package com.jetpack.compose.jetpack_compose_learn

import android.os.Bundle
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Divider
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView

/**
 * Created by harisucici on 2021/4/7
 * Class description:
 */

class MyActivity : AppCompatActivity() {
    lateinit var composeView: ComposeView
    lateinit var progressBar: LinearLayout
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my)
        val viewModel: MainModel by viewModels()
        composeView = findViewById(R.id.compose_view)
        composeView.setContent {
            Scaffold(
                topBar = {
                    AppBar(
                        context = this,
                        viewModel = viewModel,
                        titleS = viewModel.titles[viewModel.currentPage],
                        modifier = Modifier
                    )
                },
                bottomBar = {
                    HomeBottomBar(viewModel.currentPage) {
                        viewModel.currentPage = it
                        viewModel.stringS = it.toString()
                    }
                }

            ) {
//                AdapterList(data = viewModel.items) {
////                    learnConstLayout(it)
//                    Divider(
//                        modifier = Modifier
//                            .background(color = Color.Gray)
//                            .height(1.dp)
//                    )
//                }
                LazyColumn(
                    Modifier.padding(bottom = 48.dp)
                ) {
                    items(viewModel.items.size) { index ->

                        learnConstLayout(viewModel.items[index])
                        createAndroidViewForTextView(viewModel.items[index].name!!)
                        Divider(
                            modifier = Modifier
                                .background(color = Color.Gray)
                                .height(1.dp)
                        )
                    }
                }
            }
        }


    }
}

@Composable
fun createAndroidViewForTextView(myText: String) {
    val context = LocalContext.current
    val tv = remember(myText, context) {
        val x = TextView(context).apply {
//            id = R.id.text
            text = myText
            textSize = 48.0f
        }
        x
    }
    AndroidView({ tv })


}